import 'package:dio/dio.dart';

//使用方式 Global.getInstance().dio.get()

class Global {
  static Global? _instance;
  Dio? dio;

  Global() {
    dio = new Dio();
    dio!.options = BaseOptions(
        baseUrl: "www.baidu.com",
        connectTimeout: 5000,
        sendTimeout: 5000,
        receiveTimeout: 5000,
        // default headers
        headers: {"token": "1231"},
        contentType: Headers.jsonContentType,
        responseType: ResponseType.json);

    dio!.interceptors.add(InterceptorsWrapper(
        // ignore: non_constant_identifier_names
        onRequest: (options, requestInterceptorHandler) {
      print("请求" + options.headers.toString());
    }, onResponse: (options, requestInterceptorHandler) {
      print("返回" + options.headers.toString());
    }, onError: (options, requestInterceptorHandler) {
      print("错误" + options.response!.realUri.toString());
    }));
  }

  static Global getInstance() {
    if (_instance == null) _instance = Global();
    return _instance!;
  }
}
